//
//  GoodAttributeOneCell.m
//  ReceiptDelivery
//
//  Created by 吕晶 on 2020/9/23.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "GoodAttributeOneCell.h"

@interface GoodAttributeOneCell ()

@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UILabel *nameLab;
@property (nonatomic, strong) UILabel *valueLab;
@property (nonatomic, strong) UIImageView *arrowImgView;
@property (nonatomic, strong) UIButton *clickBtn;
@property (nonatomic, strong) UIView *changeBtnBGView;
@property (nonatomic, strong) UIButton *mustBtn;
@property (nonatomic, strong) UIButton *noMustBtn;
@property (nonatomic, strong) UIButton *deleteBtn;

@end

@implementation GoodAttributeOneCell

+ (instancetype)GoodAttributeOneCellWithTableView:(UITableView *)tableView {
    static NSString *cellID = @"GoodAttributeOneCell";
    GoodAttributeOneCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[GoodAttributeOneCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = Color_backgroundColor;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        self.bgView = [[UIView alloc] initWithFrame:CGRectMake(15, 10, ScreenW - 30, 60)];
        self.bgView.backgroundColor = HexColor(0xffffff);
        self.bgView.layer.masksToBounds = YES;
        self.bgView.layer.cornerRadius = 6;
        [self.contentView addSubview:self.bgView];
        
        self.nameLab = [[UILabel alloc] initWithFrame:CGRectMake(10, (self.bgView.nim_height - 15) * 0.5, self.bgView.nim_width * 0.25, 15)];
        self.nameLab.textColor = HexColor(0x2B2C44);
        self.nameLab.font = [UIFont systemFontOfSize:14];
        [self.bgView addSubview:self.nameLab];
        
        self.valueLab = [[UILabel alloc] initWithFrame:CGRectMake(self.nameLab.nim_right, self.nameLab.nim_top, self.bgView.nim_width * 0.5 - 18 - 5 - self.nameLab.nim_right, 15)];
        self.valueLab.textColor = HexColor(0x2B2C44);
        self.valueLab.font = [UIFont systemFontOfSize:14];
        self.valueLab.textAlignment = NSTextAlignmentRight;
        [self.bgView addSubview:self.valueLab];
        
        self.arrowImgView = [[UIImageView alloc] initWithFrame:CGRectMake(self.valueLab.nim_right + 5, (self.bgView.nim_height - 18) * 0.5, 18, 18)];
        [self.arrowImgView setImage:[UIImage imageNamed:@"icon_add_yundan_arrow"]];
        [self.bgView addSubview:self.arrowImgView];
        
        self.clickBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.valueLab.nim_left, self.arrowImgView.nim_top, self.arrowImgView.nim_right - self.valueLab.nim_left, self.arrowImgView.nim_height)];
        [self.clickBtn addTarget:self action:@selector(chooseClick) forControlEvents:UIControlEventTouchUpInside];
        [self.bgView addSubview:self.clickBtn];
        
        self.deleteBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.bgView.nim_width - 15 - 18, (self.bgView.nim_height - 18) * 0.5, 18, 18)];
        [self.deleteBtn setImage:[UIImage imageNamed:@"icon_goodManager_attribute_delete"] forState:0];
        [self.deleteBtn addTarget:self action:@selector(deleteClick) forControlEvents:UIControlEventTouchUpInside];
        [self.bgView addSubview:self.deleteBtn];
        
        self.changeBtnBGView = [[UIView alloc] initWithFrame:CGRectMake(self.arrowImgView.nim_right + (self.deleteBtn.nim_left - self.arrowImgView.nim_right - 72) * 0.5, (self.bgView.nim_height - 32) * 0.5, 72, 32)];
        self.changeBtnBGView.backgroundColor = Color_backgroundColor;
        self.changeBtnBGView.layer.masksToBounds = YES;
        self.changeBtnBGView.layer.cornerRadius = 16;
        [self.bgView addSubview:self.changeBtnBGView];

        self.noMustBtn = [[UIButton alloc] initWithFrame:CGRectMake(4, 4, 40, 24)];
        [self.noMustBtn setTitle:@"必填" forState:0];
        [self.noMustBtn setTitleColor:HexColor(0xffffff) forState:0];
        self.noMustBtn.titleLabel.font = BoldFontOfSize(12);
        self.noMustBtn.backgroundColor = HexColor(0xCCCCD9);
        self.noMustBtn.layer.masksToBounds = YES;
        self.noMustBtn.layer.cornerRadius = 12;
        [self.noMustBtn addTarget:self action:@selector(changeClick) forControlEvents:UIControlEventTouchUpInside];
        self.noMustBtn.hidden = NO;
        [self.changeBtnBGView addSubview:self.noMustBtn];
        
        self.mustBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.changeBtnBGView.nim_width - 4 - 40, 4, 40, 24)];
        [self.mustBtn setTitle:@"必填" forState:0];
        [self.mustBtn setTitleColor:HexColor(0xffffff) forState:0];
        self.mustBtn.titleLabel.font = BoldFontOfSize(12);
        self.mustBtn.backgroundColor = Color_ThemeColor;
        self.mustBtn.layer.masksToBounds = YES;
        self.mustBtn.layer.cornerRadius = 12;
        [self.mustBtn addTarget:self action:@selector(changeClick) forControlEvents:UIControlEventTouchUpInside];
        self.mustBtn.hidden = YES;
        [self.changeBtnBGView addSubview:self.mustBtn];
        
        self.height = self.bgView.nim_bottom;
    }
    return self;
}

- (void)changeClick {
    if (self.changeBlock) {
        self.changeBlock();
    }
}

- (void)chooseClick {
    if (self.clickBlock) {
        self.clickBlock();
    }
}

- (void)deleteClick {
    if (self.deleteBlock) {
        self.deleteBlock();
    }
}

-(void)setModel:(GoodAttributeOneModel *)model {
    _model = model;
    [self configUI];
}

- (void)configUI {
    self.nameLab.text = self.model.name;
    self.valueLab.text = self.model.currentValue;
    self.noMustBtn.hidden = [self.model.status isEqualToString:@"1"];
    self.mustBtn.hidden = ![self.model.status isEqualToString:@"1"];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
